﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgriMarketWebProj.AgriMarketService;
using System.Web.Helpers;
namespace AgriMarketWebProj.Controllers
{
    public class StatisticController : Controller
    {
        //
        // GET: /Statistic/

        public ActionResult Index()
        {
            AgriMarketWebService service = new AgriMarketWebService();
            UserAccount userAccount = null;
            if (Session["CurrentUserAccount"] != null && Session["CurrentUserAccount"].ToString() != "")
            {
                userAccount = service.GetUserAccountByUserName(Session["CurrentUserAccount"].ToString());
                ViewBag.CurrentUserAccount = userAccount;
            }
            List<YProduct> content = service.GetYearStatistic(DateTime.Now.Year).ToList();
            
            ViewBag.StartedYear = DateTime.Now.Year;
            ViewBag.FinishedYear = DateTime.Now.Year;
            return View("Index",content);
        }
        public ActionResult DrawYearStatistic(int start,int finish)
        {
            AgriMarketWebService service = new AgriMarketWebService();
            List<YProduct> content = service.GetYearStatisticInDuration(start, finish).ToList();
            System.Web.Helpers.Chart chart = new System.Web.Helpers.Chart(600, 400, theme: ChartTheme.Green).AddSeries(
            yValues: content.Select(x => x.Price).ToArray(),
            xValue: content.Select(x => x.Product.ProductName).ToArray()
                );
            var bytes = chart.GetBytes("png");
            return File(bytes, "image/png");
        }
        [HttpGet]
        public ActionResult PriceTable()
        {
            AgriMarketWebService service = new AgriMarketWebService();
            UserAccount userAccount = null;
            if (Session["CurrentUserAccount"] != null && Session["CurrentUserAccount"].ToString() != "")
            {
                userAccount = service.GetUserAccountByUserName(Session["CurrentUserAccount"].ToString());
                ViewBag.CurrentUserAccount = userAccount;
            }
            List<YProduct> content=null;
            if (ModelState.IsValid)
            {
                int startedYear = 0;
                int finishedYear = 0;
                if (!int.TryParse(Request.QueryString["StartedYear"], out startedYear))
                {
                    ModelState.AddModelError("StartedYear", "Năm bắt đầu không hợp lệ");
                    content = service.GetYearStatistic(DateTime.Now.Year).ToList();
                    ViewBag.StartedYear = DateTime.Now.Year;
                    ViewBag.FinishedYear = DateTime.Now.Year;
                    return View("Index", content);
                }
                if (!int.TryParse(Request.QueryString["FinishedYear"], out finishedYear))
                {
                    ModelState.AddModelError("FinishedYear", "Năm kết thúc không hợp lệ");
                    content=service.GetYearStatistic(DateTime.Now.Year).ToList();
                    ViewBag.StartedYear = DateTime.Now.Year;
                    ViewBag.FinishedYear = DateTime.Now.Year;
                    return View("Index", content);
                }
                content = service.GetYearStatisticInDuration(startedYear, finishedYear).ToList();
                ViewBag.StartedYear = startedYear;
                ViewBag.FinishedYear = finishedYear;
                return View("Index", content);
            }
            ViewBag.StartedYear = DateTime.Now.Year;
            ViewBag.FinishedYear = DateTime.Now.Year;
            content = service.GetYearStatistic(DateTime.Now.Year).ToList();
            return View("Index", content);
        }
        
    }
}
